$(".btnName").on("click",function () {
    var param = {startIndex:0, pageSize:3, depName:$("#queryName").val().trim(), depManger:$("#queryMangerName").val().trim()};
    var wrapper = $('#div-table-container');
    var table = $("#table-list");

    var ajaxUrl = "/student/department/getPage";
    table.dataTable().fnDestroy();//点击事件之前在清空一次
    var _table = table.dataTable($.extend(true,{},CONSTANT.DATA_TABLES.DEFAULT_OPTION, {  //_table是插件执行完
        ajax: function (data, callback, settings) {

            param.startIndex = data.start;
            console.info(param);
            $.ajax({
                type: "post",
                url: ajaxUrl,
                cache: false,
                data:param,
                contentType:  "application/x-www-form-urlencoded; charset=UTF-8",
                dataType: "json",
                success: function (result) {
                    setTimeout(function () {
                        var returnData = {};
                        returnData.draw=data.draw+1;
                        returnData.recordsTotal = result.total;
                        returnData.recordsFiltered = result.total;//每次翻页过滤的数据
                        returnData.data = result.pageData;
                        callback(returnData);
                    }, 200);
                },
                error: function (XmlHttpRequest, textStatus, errorThrown) {
                    alert("查询失败");
                }

            })
        },

        ordering: false,
        pageLength: param.pageSize,// 每页条数 设置
        columns: [
            CONSTANT.DATA_TABLES.COLUMN.CHECKBOX,
            {
                className: "ellipsis",
                data: "id",
                width: "40px",
                render: CONSTANT.DATA_TABLES.RENDER.ELLIPSIS,
            },
            {
                className: "ellipsis",
                data: "depName",
                render: CONSTANT.DATA_TABLES.RENDER.ELLIPSIS
            },
            {
                className: "ellipsis",
                data: "depMajorCount",
                width: "100px",
                render: CONSTANT.DATA_TABLES.RENDER.ELLIPSIS

            },
            {
                className: "ellipsis",
                data: "depManger",
                width: "60px",

            },
            {
                className: "ellipsis",
                data: "depBuildYear",
                width: "60px",
                render: CONSTANT.DATA_TABLES.RENDER.ELLIPSIS
            },

            {
                className: "td-operation",
                data: null,
                defaultContent: "",
                width: "200px"
            }
        ],
        "createdRow": function (row, data, index) { //创建行
            if (data.role) {
                $(row).addClass("info");
            }
            var $btnDetail = $('<button type="button" class="btn btn-default btn-ssm btn-detail">详细</button>');
            $('td', row).eq(6).append($btnDetail);

            var $btnDetail = $('<button type="button" class="btn btn-default btn-ssm btn-edit">修改</button>');
            $('td', row).eq(6).append($btnDetail);

            var $btnDetail = $('<button type="button" class="btn btn-default btn-ssm btn-delete">删除</button>');
            $('td', row).eq(6).append($btnDetail);
        },
        "drawCallback": function (settings) {
            $(":checkbox[name='cb-check-all']", wrapper).prop('checked', false);
            $("tbody tr", table).eq(0).click();
        }

    })).api();

    table.on("change", ":checkbox", function () {//给复选框添加属性
        if ($(this).is("[name='cb-check-all']")) {
            //全选
            $(":checkbox", table).prop("checked", $(this).prop("checked"));
        } else {
            //一般复选
            var checkbox = $("tbody :checkbox", table);//获取所有表格里的checkbox
            $(":checkbox[name='cb-checkbox']", table).prop("checked", checkbox.length == checkbox.filter(":checkd").length);
        }
    }).on("click", ".td-checkbox", function (event) {
        //点击单元格复选按钮
        !$(event.target).is(":checkbox") && $(":checkbox", this).trigger("click");
    }).on("click", ".btn-edit", function () {
        //点击修改按钮
        var item = _table.row($(this).closest('tr')).data();//拿到该行中的tr中的值  提取成json对象，这是datatable自定义的
        $("#id-edit").val(item.id);
        $("#departmentName-edit").val(item.depName);
        $("#departmentMajorCount-edit").val(item.depMajorCount);
        $("#departmentManger-edit").val(item.depManger);
        $("#departmentBuildYear-edit").val(item.depBuildYear);
        $("#myModal-edit").modal("show");
    }).on('click', ".btn-delete", function () {
        //点击删除按钮
        var item = _table.row($(this).closest('tr')).data();
        var flag = confirm("确定删除该学院记录吗");
        console.info(flag);
        var objp = {'id': item.id};

        if (flag) {
            $.ajax({
                type: "post",
                url: "/student/department/deleteDep",
                cache: false,
                data: objp,
                contentType: "application/x-www-form-urlencoded;charset:UTF-8",
                dataType: "json",

                success: function (result) {
                    console.info(result);
                    if (result.success) {
                        _table.draw(false);
                    } else {
                        alert(result.message);
                    }
                },
                error: function (XmlHttpRequest, textStatus, errorThrown) {
                    alert("删除失败");
                }
            })
        }
    }).on('click', ".btn-detail", function () {
        var item = _table.row($(this).closest('tr')).data();
        $("#departmentId-detail").val(item.id);
        $("#departmentName-detail").val(item.depName);
        $("#departmentMajorCount-detail").val(item.depMajorCount);
        $("#departmentManger-detail").val(item.depManger);
        $("#departmentBuildYear-detail").val(item.depBuildYear);
        $("#myModal-detail").modal("show");
    });

    $("#add-department-submit").on("click", function () {//绑定点击事件
        var objp = {};
        objp.depName = $("#departmentName-add").val().trim();
        objp.depMajorCount = $("#departmentMajorCount-add").val().trim();
        objp.depManger = $("#departmentManger-add").val().trim();
        objp.depBuildYear = $("#departmentBuildYear-add").val().trim();

        if (objp.departmentName == "") {
            alert("学院信息不能为空");
            return;
        }
        $.ajax({
            type: "post",
            url: "/student/department/saveDep",
            cache: false,
            data: objp,
            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            dataType: "json",
            success: function (result) {
                console.info(result);
                if (result.success) {
                    _table.draw(false);//刷新表格
                    $('#myModal-add').modal('hide');
                    document.getElementById('add-department-form').reset();
                } else {
                    alert(result.message);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("保存失败");
            }
        })
    });

        $("#edit-department-submit").on("click", function () {//绑定点击事件
            var objp={};
            objp.id = $('#id-edit').val();
            objp.depName = $("#departmentName-edit").val().trim();
            objp.depMajorCount = $("#departmentMajorCount-edit").val().trim();
            objp.depManger = $("#departmentManger-edit").val().trim();
            objp.depBuildYear = $("#departmentBuildYear-edit").val().trim();

            if(objp.id=="" || objp.id < 0){
                alert("无法修改，记录主键丢失");
                return;
            }

            if(objp.depName==""){
                alert("学院名不能为空");
                return;
            }
            console.info(objp)

            $.ajax({
                type: "post",
                url: "/student/department/updateDep",
                cache: false,
                data: objp,
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                dataType: "json",
                success: function (result) {
                    console.info(result);
                    if (result.success) {
                        _table.draw(false);//刷新表格
                        $('#myModal-add').modal('hide');
                        document.getElementById('edit-department-form').reset();
                    } else {
                        alert(result.message);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("保存失败");
                }
            });
        })

})







